package me.ivenapp.gomoku.component;

import me.ivenapp.gomoku.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.media.image.PixelMap;

public class TitleBar extends ComponentContainer {
    private final Text txtTitle;
    private final Text txtSubtitle;
    private final Image iconLeft;
    ComponentContainer cntAction;

    public TitleBar(Context context, AttrSet attrSet) {
        super(context, attrSet);

        Component layout = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_titlebar, this, true);

        if(attrSet.getAttr("background_element").isPresent()){
            layout.findComponentById(ResourceTable.Id_title_bar_root).setBackground(attrSet.getAttr("background_element").get().getElement());
        }

        txtTitle = (Text) layout.findComponentById(ResourceTable.Id_txt_title);
        if(attrSet.getAttr("title").isPresent()) {
            setTitle(attrSet.getAttr("title").get().getStringValue());
        }

        if(attrSet.getAttr("title_color").isPresent()) {
            setTitleColor(attrSet.getAttr("title_color").get().getColorValue());
        }

        txtSubtitle = (Text) layout.findComponentById(ResourceTable.Id_txt_subtitle);
        if(attrSet.getAttr("subtitle").isPresent()) {
            setSubtitle(attrSet.getAttr("subtitle").get().getStringValue());
        } else {
            txtSubtitle.setVisibility(Component.HIDE);
        }

        if(attrSet.getAttr("subtitle_color").isPresent()) {
            setSubtitleColor(attrSet.getAttr("subtitle_color").get().getColorValue());
        }

        iconLeft = (Image) layout.findComponentById(ResourceTable.Id_icon_left);
        Element elm = null;
        if(attrSet.getAttr("left_icon_src").isPresent()) {
            elm = attrSet.getAttr("left_icon_src").get().getElement();
            setLeftIconElement(elm);
            setLeftIcon(Component.VISIBLE);
        } else {
            iconLeft.setVisibility(Component.HIDE);
        }

        if(attrSet.getAttr("back_icon").isPresent()) {
            String v = attrSet.getAttr("back_icon").get().getStringValue().toLowerCase();
            int visible = "true".equals(v) ? Component.VISIBLE : Component.HIDE;
            setBackIconElement(visible, elm);
        }

        if(attrSet.getAttr("show_divider").isPresent()) {
            String v = attrSet.getAttr("show_divider").get().getStringValue().toLowerCase();
            int visible = "true".equals(v) ? Component.VISIBLE : Component.HIDE;
            setDividerVisible(visible);
        }

        cntAction = (ComponentContainer) findComponentById(ResourceTable.Id_cnt_action_icons);
        cntAction.setVisibility(Component.HIDE);
    }

    /**
     * 设置内容区宽度
     * @param width
     */
    public void setMainWidth(int width) {
        findComponentById(ResourceTable.Id_title_bar_cnt_main).setWidth(width);
    }

    public void setTitle(String title) {
        txtTitle.setVisibility(Component.VISIBLE);
        txtTitle.setText(title);
    }

    public void setTitleColor(Color color) {
        txtTitle.setTextColor(color);
    }

    public void setSubtitle(String subtitle) {
        txtSubtitle.setVisibility(Component.VISIBLE);
        txtSubtitle.setText(subtitle);
    }

    public void setSubtitleColor(Color color) {
        txtSubtitle.setTextColor(color);
    }

    /**
     * 设置返回键。如果设置了返回键就不需要 setIconLeft
     * @param visible
     * @param pixelMap
     */
    public void setBackIconPixelMap(int visible, PixelMap pixelMap) {
        setLeftIcon(visible);
        if(visible == Component.VISIBLE) {
            if(pixelMap == null) {
                setBackIconElement(visible, null);
            } else {
                setLeftIconPixelMap(pixelMap);
                setBackIconClickListener();
            }
        }
    }

    public void setBackIconElement(int visible, Element element) {
        setLeftIcon(visible);
        if(visible == Component.VISIBLE) {
            if (element == null) {
                element = new VectorElement(getContext(), ResourceTable.Graphic_ic_public_back);
            }
            setLeftIconElement(element);
            setBackIconClickListener();
        }
    }

    private void setBackIconClickListener(){
        setLeftIconClickedListener(component -> {
            Context context = getContext();
            if(context instanceof AbilitySlice) {
                ((AbilitySlice)context).terminate();
            } else {
                context.terminateAbility();
            }
        });
    }

    public void setLeftIcon(int visible) {
        iconLeft.setVisibility(visible);
    }
    public void setLeftIconPixelMap(PixelMap pixelMap) {
        iconLeft.setPixelMap(pixelMap);
    }
    public void setLeftIconElement(Element element) {
        iconLeft.setImageElement(element);
    }

    public void setLeftIconClickedListener(ClickedListener listener) {
        iconLeft.setClickedListener(listener);
    }

    /**
     * 添加右边操作按钮
     * @param elements
     * @param listener
     */
    public void addActionIcon(Element[] elements, ActionClickedListener listener) {
        cntAction.removeAllComponents();
        for(int i=0; i<elements.length; i++) {
            Image img = createActionIconImage();
            img.setImageElement(elements[i]);
            img.setClickable(true);
            int finalI = i;
            img.setClickedListener(component -> listener.onClick(img, finalI));
            cntAction.addComponent(img);
        }
        if(elements.length > 0) {
            cntAction.setVisibility(Component.VISIBLE);
        }
    }

    public void setActionIconVisibility(int visibility) {
        cntAction.setVisibility(visibility);
    }

    private Image createActionIconImage() {
        Image img = new Image(getContext());
        try {
            int size = AttrHelper.vp2px(24, getContext());
            int leftMargin = AttrHelper.vp2px(16, getContext());;
            img.setWidth(size);
            img.setHeight(size);
            img.setMarginLeft(leftMargin);
            img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return img;
    }

    public void setDividerVisible(int visible) {
        findComponentById(ResourceTable.Id_title_bar_divider).setVisibility(visible);
    }

    public interface ActionClickedListener {
        void onClick(Component component, int i);
    }
}
